<?php

/**
 * about actions.
 *
 * @package    lezgro
 * @subpackage about
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class contactsActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->bg_class = 'bg_contacts';
        $this->page_title = 'Lezgro - Contacts';
        $this->form = new FeedbackForm();
        
        if($request->isXmlHttpRequest()) {
            $response = array();
            $response['html'] = $this->getPartial('index');
            $response['title'] = $this->page_title;
            $response['bg_class'] = $this->bg_class;
            $response['form'] = $this->form;
            
            return $this->renderText(json_encode($response));
        }
        
        $this->getResponse()->setTitle($this->page_title);
    }
   
    public function executeAjaxFeedbackFormSave(sfWebRequest $request) {
        if($request->isXmlHttpRequest()) {
            $response = array(
                'status' => FALSE
            );
            
            $form = new FeedbackForm();
            $this->processForm($request, $form);
            
            if($form->hasErrors()) {
                $form->processErrorsForAjaxResponse($response);
            } else {
                sfProjectConfiguration::getActive()->loadHelpers('I18N');
                
                $response['success_message'] = __('Сообщение успешно отправлено') . '!';
                $response['status'] = TRUE;
            }
            
            return $this->renderText(json_encode($response));
        }
    }
    
    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        
        if($form->isValid()) {
            $feedback = $form->save();
            
            return $feedback;
        }
        
        return false;
    }
}